package org.example;

import org.example.model.JdbcInfo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JdbcUrlParser {
    public static void main(String[] args) {
        // 示例 JDBC URL
        String mysqlUrl = "jdbc:mysql://172.30.36.144:9031/etl_ods?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf-8";
        String sqlServerUrl = "jdbc:sqlserver://172.30.36.154:1433;databaseName=SurveyAndDesign;encrypt=true;trustServerCertificate=true";
        String sqlServerUrl2 = "jdbc:sqlserver://172.30.36.224;instanceName=SPZXZZ;databaseName=CCCV_ProjectSupervision;encrypt=false;trustServerCertificate=true";


//        String postgresUrl = "jdbc:postgresql://192.168.1.1:5432/mydatabase?ssl=true";
//        String oracleUrl = "jdbc:oracle:thin:@192.168.1.2:1521:ORCL";
        System.out.println( parseJdbcUrl(sqlServerUrl));

        System.out.println( parseJdbcUrl(sqlServerUrl2));

    }

    public static JdbcInfo parseJdbcUrl(String jdbcUrl) {
        jdbcUrl = jdbcUrl.trim();
        if (jdbcUrl.startsWith("jdbc:mysql:")) {
           return extractMysqlDetails(jdbcUrl);
        } else if (jdbcUrl.startsWith("jdbc:sqlserver:")) {
            return extractSqlServerDetails(jdbcUrl);
        } else if (jdbcUrl.startsWith("jdbc:postgresql:")) {
            return extractPostgresDetails(jdbcUrl);
        } else if (jdbcUrl.startsWith("jdbc:oracle:")) {
            return  extractOracleDetails(jdbcUrl);
        } else {
        }
        return null;
    }

    private static JdbcInfo extractMysqlDetails(String jdbcUrl) {
        // 正则匹配 MySQL URL 格式
        String regex = "jdbc:mysql://([\\d\\.]+):(\\d+)/(\\w+)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(jdbcUrl);

        if (matcher.find()) {

            JdbcInfo jdbcInfo = new JdbcInfo();
            jdbcInfo.setIp(matcher.group(1));
            jdbcInfo.setPort(matcher.group(2));
            jdbcInfo.setDatabase(matcher.group(3));
            jdbcInfo.setType("mysql");
            return jdbcInfo;
        } else {
            System.out.println("Invalid MySQL JDBC URL");
        }

        return null;
    }

    private static JdbcInfo extractSqlServerDetails(String jdbcUrl) {
        // 正则匹配 SQL Server URL 格式，包含端口或没有端口
        String regexWithPort = "jdbc:sqlserver://([\\d\\.]+):(\\d+);databaseName=([^;]+)";
        String regexWithoutPort = "jdbc:sqlserver://([\\d\\.]+);instanceName=([^;]+);databaseName=([^;]+)";

        Pattern patternWithPort = Pattern.compile(regexWithPort);
        Matcher matcherWithPort = patternWithPort.matcher(jdbcUrl);

        if (matcherWithPort.find()) {
            // 如果 URL 中有端口
            JdbcInfo jdbcInfo = new JdbcInfo();
            jdbcInfo.setIp(matcherWithPort.group(1));
            jdbcInfo.setPort(matcherWithPort.group(2));
            jdbcInfo.setDatabase(matcherWithPort.group(3));
            jdbcInfo.setType("sqlserver");
            return jdbcInfo;
        } else {
            // 如果 URL 中没有端口，使用默认端口
            Pattern patternWithoutPort = Pattern.compile(regexWithoutPort);
            Matcher matcherWithoutPort = patternWithoutPort.matcher(jdbcUrl);
            if (matcherWithoutPort.find()) {
                JdbcInfo jdbcInfo = new JdbcInfo();
                jdbcInfo.setIp(matcherWithoutPort.group(1));
                jdbcInfo.setPort("1433");
                jdbcInfo.setDatabase(matcherWithoutPort.group(3));
                jdbcInfo.setType("sqlserver");
                return jdbcInfo;
            } else {
                System.out.println("Invalid SQL Server JDBC URL");
            }
        }
        return null;
    }

    private static JdbcInfo extractPostgresDetails(String jdbcUrl) {
        // 正则匹配 PostgreSQL URL 格式
        String regex = "jdbc:postgresql://([\\d\\.]+):(\\d+)/(\\w+)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(jdbcUrl);

        if (matcher.find()) {

            JdbcInfo jdbcInfo = new JdbcInfo();
            jdbcInfo.setIp(matcher.group(1));
            jdbcInfo.setPort(matcher.group(2));
            jdbcInfo.setDatabase(matcher.group(3));
            jdbcInfo.setType("pgsql");

            return jdbcInfo;
        } else {
            System.out.println("Invalid PostgreSQL JDBC URL");
        }
        return null;

    }

    private static JdbcInfo extractOracleDetails(String jdbcUrl) {
        // 正则匹配 Oracle URL 格式
        String regex = "jdbc:oracle:thin:@([\\d\\.]+):(\\d+):([\\w]+)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(jdbcUrl);

        if (matcher.find()) {

            JdbcInfo jdbcInfo = new JdbcInfo();
            jdbcInfo.setIp(matcher.group(1));
            jdbcInfo.setPort(matcher.group(2));
            jdbcInfo.setDatabase(matcher.group(3));
            jdbcInfo.setType("oracle");

            return jdbcInfo;
        } else {
            System.out.println("Invalid Oracle JDBC URL");
        }
        return  null;
    }
}
